<template>
  <div class="cost">
    <el-card>
      <el-form inline>
        <el-form-item label="企业名称">
          <el-input size="small" placeholder="请输入企业名称" v-model="pageParams.enterpriseName"></el-input>
        </el-form-item>
        <el-form-item label="缴费时间">
          <el-date-picker size="small" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" v-model="time" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="small" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button size="small" icon="el-icon-plus" type="success" @click="handleAdd">添加账单</el-button>

      <el-table size="small" border stripe :header-cell-style="{background:'#4d8ed9',color:'#fff'}" :data="propertyfeeList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="账单编号" prop="billNumber" align="center"></el-table-column>
        <el-table-column label="企业名称" prop="enterpriseName" align="center"></el-table-column>
        <el-table-column label="租赁楼宇" prop="buildingName" align="center"></el-table-column>
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" align="center"></el-table-column>
        <el-table-column label="账单金额(元)" prop="paymentAmount" align="center"></el-table-column>
        <el-table-column label="缴费时间" prop="createTime" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleShow(row)">查看</el-button>
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="pageParams.page" :page-sizes="[15, 20, 30, 40]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total" background
      ></el-pagination>

    </el-card>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%">
      <fee-form v-if="addFee" @success="handleSuccess" :edit="edit" ref="feeForm"></fee-form>
      <fee-pay v-else ref="feePay"></fee-pay>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import FeeForm from '@/views/propety/components/feeForm/feeForm.vue'
import FeePay from '@/views/propety/components/feePay/feePay.vue'

export default {
  name: 'cost',
  components: { FeePay, FeeForm },
  data() {
    return {
      /* 获取物业费的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 15,
        enterpriseName: undefined,
        start: undefined,
        end: undefined
      },
      time: [],
      /* 加载物业费列表的loading状态 */
      loading: false,
      /* 是否显示对话框组件 */
      dialogVisible: false,
      /* 是否是添加账单还是查看账单 */
      addFee: true,
      /* 是否是编辑状态 */
      edit: false
    }
  },
  methods: {
    /* 获取物业费列表的回调函数 */
    async fetchPropertyfeeList() {
      try {
        this.loading = true
        await this.$store.dispatch('cost/fetchPropertyfeeList', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* pageSize 改变时会触发 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchPropertyfeeList()
    },
    /* currentPage 改变时会触发 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchPropertyfeeList()
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.enterpriseName}, 是否继续?`, '提示', { type: 'warning' }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('cost/deletePropertyfee', row.id)
        this.$message.success(deleteResult)
        this.fetchPropertyfeeList()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击搜索按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.pageParams.start = this.time[0]
      this.pageParams.end = this.time[1]
      this.fetchPropertyfeeList()
    },
    /* 添加添加按钮的回调函数 */
    handleAdd() {
      this.addFee = true
      this.edit = false
      this.dialogVisible = true
    },
    /* 添加编辑按钮的回调函数 */
    handleEdit(row) {
      this.addFee = true
      this.edit = true
      this.dialogVisible = true

      this.$nextTick(()=>{
        this.$refs.feeForm.findFee(row)
      })
    },
    /* 添加查看按钮的回调函数 */
    handleShow(row) {
      this.addFee = false
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.feePay.findPayInfo(row)
      })
    },
    /* 子组件fee-form添加账单成功的自定义事件 */
    handleSuccess() {
      this.dialogVisible = false
      this.fetchPropertyfeeList()
    }
  },
  computed: {
    ...mapGetters('cost', ['propertyfeeList', 'total']),
    title() {
      return this.addFee ? `${this.edit ? '编辑账单' : '添加账单'}` : '查看账单'
    }
  },
  mounted() {
    this.fetchPropertyfeeList()
  }
}
</script>

<style scoped lang="scss">
.cost {
  .el-card {
    margin-bottom: 10px;

    .el-form-item {
      margin-bottom: unset;
    }

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }

  }

  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
  }
}
</style>

